package cn.lotusnut.rabbitmqbaseapi.util;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @date: 2022/1/25 16:07
 * @author: JiangMing Zhou
 * @since: JDK1.8
 */
@Component
public class RabbitMqUtil {
    @Value("${rabbitmq.host}")
    private String host;
    @Value("${rabbitmq.virtualHost}")
    private String virtualHost;
    @Value("${rabbitmq.port}")
    private int port;
    @Value("${rabbitmq.username}")
    private String username;
    @Value("${rabbitmq.password}")
    private String password;
    private static ConnectionFactory connectionFactory;

    @PostConstruct
    public void init(){
        connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setVirtualHost(virtualHost);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
    }

    public ConnectionFactory getConnectionFactory() {
        return connectionFactory;
    }

    public Connection getConnection() throws IOException, TimeoutException {
        return connectionFactory.newConnection();
    }
}
